<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加优惠券</title>
    <link rel="stylesheet" href="../../layui/css/layui.css"/>
    <script src="../../layui/layui.js"></script>
    <script>
        layui.use('element', function() {
            var $ = layui.jquery;  //加载jQuery模块
            var element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
        });
    </script>
</head>
<body>

<form class="layui-form">
    <div class="layui-form-item">
        <input type="hidden" name="id"  class="layui-input" >
        <input type="hidden" name="inUsed"  class="layui-input" >
        <input type="hidden" name="image" class="image" class="layui-input" >
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">服装品牌</label>
        <div class="layui-input-inline">
            <input type="text" name="brand" placeholder="" required lay-verify="required" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">服装编号</label>
        <div class="layui-input-inline">
            <input type="text" name="code" placeholder="" required lay-verify="required" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">服装品类</label>
        <div class="layui-input-inline">
            <input type="text" name="type" placeholder="" required lay-verify="required" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">X码数量</label>
        <div class="layui-input-inline">
            <input type="number" name="countsX" placeholder="" required lay-verify="required" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">M码数量</label>
        <div class="layui-input-inline">
            <input type="number" name="countsM" placeholder="" required lay-verify="required" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">L码数量</label>
        <div class="layui-input-inline">
            <input type="number" name="countsL" placeholder="" required lay-verify="required" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格（元）</label>
        <div class="layui-input-inline">
            <input type="text" name="price" placeholder="" required lay-verify="required" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">服装介绍</label>
        <div class="layui-input-inline">
            <textarea name="desc" class="layui-textarea" readonly></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-upload">
            <label class="layui-form-label">样图展示</label>
            <div id="layer-photos-demo" class="layer-photos-demo">
                <img src="" layer-src="" name="sokImg" id="demo1" style="width: 200px;height: 150px" title="查看大图">
                <p id="demoText"></p>
            </div>
        </div>
    </div>
</form>


<script>
    layui.use(['upload', 'element', 'layer','form','laydate'], function() {
        var $ = layui.jquery
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer
            , form = layui.form
            ,laydate = layui.laydate;





        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/uploadImage' //改成您自己的上传接口
            ,field:"image"
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                //$(".layui-form").append("<input  class='image' type='hidden' name='image' value='"+res.data+"'>");
                $(".image").val(res.data)
            }
        });

        // 表单提交(异步)
        form.on('submit(add)', function(data){

            //获取隐藏域中的图片
            data.field.img=$(".image")[0].value;
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}

            $.ajax({
                url:"/coupon/update"
                ,method:"post"
                ,data:data.field
                ,type:'post'
                ,success:function(res){
                    console.log(res)
                    if(res.code == 0){

                        layer.msg(res.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            window.parent.location.reload()
                        });

                    }else{
                        layer.msg(res.msg)
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })

    layer.photos({
        photos: '#layer-photos-demo'
        ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });
</script>

</body>
</html>